package com.passwordmanager.controller;

import com.passwordmanager.dto.password.PasswordEntryDto;
import com.passwordmanager.dto.password.PasswordGenerateRequest;
import com.passwordmanager.model.PasswordStrengthCheck;
import com.passwordmanager.service.PasswordService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/passwords")
@RequiredArgsConstructor
public class PasswordController {

    private final PasswordService passwordService;

    @PostMapping("/generate")
    public ResponseEntity<String> generatePassword(@RequestBody PasswordGenerateRequest request) {
        return ResponseEntity.ok(passwordService.generatePassword(request));
    }

    @PostMapping("/check-strength")
    public ResponseEntity<PasswordStrengthCheck> checkPasswordStrength(@RequestParam String password) {
        return ResponseEntity.ok(passwordService.checkPasswordStrength(password));
    }

    @PostMapping
    public ResponseEntity<PasswordEntryDto> createPassword(
            @Valid @RequestBody PasswordEntryDto passwordDto,
            Authentication authentication) {
        return ResponseEntity.ok(passwordService.createPassword(passwordDto, authentication.getName()));
    }

    @PutMapping("/{id}")
    public ResponseEntity<PasswordEntryDto> updatePassword(
            @PathVariable Long id,
            @Valid @RequestBody PasswordEntryDto passwordDto,
            Authentication authentication) {
        return ResponseEntity.ok(passwordService.updatePassword(id, passwordDto, authentication.getName()));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePassword(
            @PathVariable Long id,
            Authentication authentication) {
        passwordService.deletePassword(id, authentication.getName());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<PasswordEntryDto> getPassword(
            @PathVariable Long id,
            Authentication authentication) {
        return ResponseEntity.ok(passwordService.getPassword(id, authentication.getName()));
    }

    @GetMapping
    public ResponseEntity<List<PasswordEntryDto>> getAllPasswords(Authentication authentication) {
        return ResponseEntity.ok(passwordService.getAllPasswords(authentication.getName()));
    }

    @PostMapping("/{id}/share")
    public ResponseEntity<Void> sharePassword(
            @PathVariable Long id,
            @RequestParam String shareWithUsername,
            Authentication authentication) {
        passwordService.sharePassword(id, shareWithUsername, authentication.getName());
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}/share")
    public ResponseEntity<Void> removePasswordShare(
            @PathVariable Long id,
            @RequestParam String shareWithUsername,
            Authentication authentication) {
        passwordService.removePasswordShare(id, shareWithUsername, authentication.getName());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/shared")
    public ResponseEntity<List<PasswordEntryDto>> getSharedPasswords(Authentication authentication) {
        return ResponseEntity.ok(passwordService.getSharedPasswords(authentication.getName()));
    }
} 